package study.kit.uitls;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class DateUtils {


    /**
     * @param d1 Date
     * @param d2 Date
     * @return 29h34min
     */
    public static String formatBetween(Date d1, Date d2) {
        //
        if (!ObjectUtil.isAllNotEmpty(d1, d2)) {
            return StrUtil.EMPTY;
        }

        //
        long totalMin = DateUtil.between(d1, d2, DateUnit.MINUTE);
        log.info("共 {} 分", totalMin);

        // 小时
        String hourText = StrUtil.EMPTY;
        long hour = totalMin / 60;
        if (hour != 0) {
            hourText = StrUtil.format("{}h", hour);
        }
        // 分钟
        String minText = StrUtil.EMPTY;
        long min = totalMin % 60;
        if (min != 0) {
            minText = StrUtil.format("{}min", min);
        }
        return StrUtil.format("{}{}", hourText, minText);
    }
}
